package com.rs.adminweb.dept.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.rs.commons.web.AdminBaseAction;
import com.rs.rsbiz.dept.delegate.SecCatalogDelegate;

public class SecCatalogAction extends AdminBaseAction
{
    private SecCatalogDelegate secCatalogDelegate;

    public SecCatalogDelegate getSecCatalogDelegate()
    {
        return secCatalogDelegate;
    }

    public void setSecCatalogDelegate(SecCatalogDelegate secCatalogDelegate)
    {
        this.secCatalogDelegate = secCatalogDelegate;
    }

    public ModelAndView getParentCatalogs(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();

        List list = secCatalogDelegate.getParentCatalogs();

        if (null != list)
        {
            for (int i = 0; i < list.size(); i++)
            {
                Row row = (Row) list.get(i);
                JSONObject jo = new JSONObject();
                jo.element("id", row.getString("id"));
                jo.element("name", row.getString("name"));
                json.add(jo);
            }

        }

        this.doResponse(request, response, json);
        return null;
    }

    /**
     * 获取医院的子科室列表，是根据医院来的，从t_section表中取，不能从t_catalog中取
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView getChildSecsOfHos(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();
        if (params.containsKey("parent_cat_id")&&params.containsKey("hospital_id"))
        {
            long parentId = params.getLong("id");
            List list = secCatalogDelegate.getChildSecsOfHos(params);

            if (null != list)
            {
                List jsonList = new ArrayList();
                for (int i = 0; i < list.size(); i++)
                {
                    Row row = (Row) list.get(i);
                    JSONObject jo = new JSONObject();
                    jo.element("id", row.getString("id"));
                    jo.element("name", row.getString("name"));
                    json.add(jo);
                }

            }
        }
        this.doResponse(request, response, json);
        return null;
    }
    
    public ModelAndView getCatalogOfParent(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();
        if (params.containsKey("id"))
        {
            long parentId = params.getLong("id");
            List list = secCatalogDelegate.getCatalogOfParent(parentId);

            if (null != list)
            {
                List jsonList = new ArrayList();
                for (int i = 0; i < list.size(); i++)
                {
                    Row row = (Row) list.get(i);
                    JSONObject jo = new JSONObject();
                    jo.element("id", row.getString("id"));
                    jo.element("name", row.getString("name"));
                    json.add(jo);
                }

            }
        }
        this.doResponse(request, response, json);
        return null;
    }

    public ModelAndView getAllCatalog(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);

        Row reuslt = secCatalogDelegate.getAllCatalog();

        return null;
    }

    public void doResponse(HttpServletRequest request, HttpServletResponse response, JSONArray json)
    {
        try
        {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

    public void doResponse(HttpServletRequest request, HttpServletResponse response, JSONObject json)
    {
        try
        {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }
}
